﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.IBUS;
using SD.BUS;
using SD.Entities;
using System.Collections.Generic;

namespace SD.UI
{
    public partial class AddAddress : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Username"] != null)
            {
                if (!Page.IsPostBack)
                {
                    BindCountry();
                    BindCounty();
                    BindTown();
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            IAddressBIZ addressBIZ = new AddressBIZ();
            Address address = new Address();

            address.Line1 = txtLine1.Text;
            address.Line2 = txtLine2.Text;
            address.Line3 = txtLine3.Text;
            address.PostCode = txtPostcode.Text;

            Town t = new Town();
            t.TownID = ddlTown.SelectedValue;
            address.Town= t;

            County c = new County();
            c.CountyID = ddlCounty.SelectedValue;
            address.County = c;

            Country ct = new Country();
            ct.CountryID = ddlCountry.SelectedValue;
            address.Country = ct;

            bool isSuccessful = addressBIZ.Insert(address);

            if (isSuccessful)
            {
                ScriptManager.RegisterStartupScript(this, typeof(Button), "", "alert('Insert Success');window.close();", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, typeof(Button), "", "alert('Insert fail');", true);
            }
            //Response.Redirect("SearchAddress.aspx");
            
        }

        private void BindCountry()
        {
            ICountryBIZ countryBIZ = new CountryBIZ();
            List<Country> listCountry = countryBIZ.GetAll();

            DataTable dt = new DataTable();
            dt.Columns.Add("CountryID");
            dt.Columns.Add("Name");

            if (listCountry.Count > 0)
            {
                foreach (Country c in listCountry)
                {
                    dt.Rows.Add(c.CountryID, c.Name);
                }
            }
            dt.DefaultView.Sort = "Name";
            ddlCountry.DataSource = dt;
            ddlCountry.DataTextField = "Name";
            ddlCountry.DataValueField = "CountryID";
            ddlCountry.DataBind();
        }

        private void BindCounty()
        {
            ICountyBIZ countyBIZ = new CountyBIZ();
            List<County> listCounty = countyBIZ.GetAll();

            DataTable dt = new DataTable();
            dt.Columns.Add("CountyID");
            dt.Columns.Add("Name");

            if (listCounty.Count > 0)
            {
                foreach (County c in listCounty)
                {
                    dt.Rows.Add(c.CountyID, c.Name);
                }
            }

            dt.DefaultView.Sort = "Name";
            ddlCounty.DataSource = dt;
            ddlCounty.DataTextField = "Name";
            ddlCounty.DataValueField = "CountyID";
            ddlCounty.DataBind();
        }

        private void BindTown()
        {
            ITownBIZ townBIZ = new TownBIZ();
            List<Town> listTown = townBIZ.GetAll();

            DataTable dt = new DataTable();
            dt.Columns.Add("TownID");
            dt.Columns.Add("Name");

            if (listTown.Count > 0)
            {
                foreach (Town t in listTown)
                {
                    dt.Rows.Add(t.TownID, t.Name);
                }
            }

            dt.DefaultView.Sort = "Name";
            ddlTown.DataSource = dt;
            ddlTown.DataTextField = "Name";
            ddlTown.DataValueField = "TownID";
            ddlTown.DataBind();
        }

        private void Reset()
        {
            txtLine1.Text = "";
            txtLine2.Text = "";
            txtLine3.Text = "";
            txtPostcode.Text = "";
            ddlTown.SelectedIndex = 0;
            ddlCounty.SelectedIndex = 0;
            ddlCountry.SelectedIndex = 0;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
    }
}
